package ma.cdm.compteRendu.dao;
// default package


import static org.hibernate.criterion.Example.create;

import java.util.List;

import ma.cdm.compteRendu.bo.PersonneExterne;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 	* A data access object (DAO) providing persistence and search support for PersonneExterne entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .PersonneExterne
  * @author Limouri Anouar 
 */
    @Transactional   
public class PersonneExterneDAO  {
	     private static final Logger log = LoggerFactory.getLogger(PersonneExterneDAO.class);
		//property constants
	public static final String NOM = "nom";
	public static final String PRENOM = "prenom";
	public static final String CIVILITE = "civilite";
	public static final String MAIL = "mail";
	public static final String TELEPHONE = "telephone";
	public static final String FONCTION = "fonction";
	public static final String SOCIETE = "societe";



    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory){
       this.sessionFactory = sessionFactory;
    }

    private Session getCurrentSession(){
     return sessionFactory.getCurrentSession(); 
    }
	protected void initDao() {
		//do nothing
	}
    
    public void save(PersonneExterne transientInstance) {
        log.debug("saving PersonneExterne instance");
        try {
            getCurrentSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(PersonneExterne persistentInstance) {
        log.debug("deleting PersonneExterne instance");
        try {
            getCurrentSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public PersonneExterne findById( java.lang.Long id) {
        log.debug("getting PersonneExterne instance with id: " + id);
        try {
            PersonneExterne instance = (PersonneExterne) getCurrentSession()
                    .get(PersonneExterne.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public void addParticipantExterneToCompteRendu(Long reportId  ,Long participantExterne)
    {
    	try {
            String queryString = "insert into participantexterne (IDCOMPTERENDU,IDPERSONNEEXTERNE) VALUES (?,?)";
            Query queryObject = getCurrentSession().createSQLQuery(queryString);
   		    queryObject.setLong(0, reportId);
   		    queryObject.setLong(1, participantExterne);
   		    queryObject.executeUpdate();
         } catch (RuntimeException re) {
            log.error("addParticipantExterneToCompteRendu failed", re);
            throw re;
         }
    }
    
    @SuppressWarnings("unchecked")
	public List<PersonneExterne> findByExample(PersonneExterne instance) {
        log.debug("finding PersonneExterne instance by example");
        try {
            List<PersonneExterne> results = (List<PersonneExterne>) getCurrentSession() .createCriteria(PersonneExterne.class).add( create(instance) ).list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    @SuppressWarnings("unchecked")
	public List<PersonneExterne> findByProperty(String propertyName, Object value) {
      log.debug("finding PersonneExterne instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from PersonneExterne as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getCurrentSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List<PersonneExterne> findByNom(Object nom
	) {
		return findByProperty(NOM, nom
		);
	}
	
	public List<PersonneExterne> findByPrenom(Object prenom
	) {
		return findByProperty(PRENOM, prenom
		);
	}
	
	
	public List<PersonneExterne> findByCivilite(Object civilite
	) {
		return findByProperty(CIVILITE, civilite
		);
	}
	
	
	public List<PersonneExterne> findByMail(Object mail
	) {
		return findByProperty(MAIL, mail
		);
	}
	

	public List<PersonneExterne> findByTelephone(Object telephone
	) {
		return findByProperty(TELEPHONE, telephone
		);
	}
	

	public List<PersonneExterne> findByFonction(Object fonction
	) {
		return findByProperty(FONCTION, fonction
		);
	}
	

	public List<PersonneExterne> findBySociete(Object societe
	) {
		return findByProperty(SOCIETE, societe
		);
	}
	

	@SuppressWarnings({ "unchecked" })
	public List<PersonneExterne> findAll() {
		log.debug("finding all PersonneExterne instances");
		try {
			String queryString = "from PersonneExterne";
	         Query queryObject = getCurrentSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public PersonneExterne merge(PersonneExterne detachedInstance) {
        log.debug("merging PersonneExterne instance");
        try {
            PersonneExterne result = (PersonneExterne) getCurrentSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(PersonneExterne instance) {
        log.debug("attaching dirty PersonneExterne instance");
        try {
            getCurrentSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(PersonneExterne instance) {
        log.debug("attaching clean PersonneExterne instance");
        try {
                      	getCurrentSession().buildLockRequest(LockOptions.NONE).lock(instance);
          	            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static PersonneExterneDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (PersonneExterneDAO) ctx.getBean("personneExterneDAO");
	}
}